﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Configuration;
using System.IO;

namespace DreamIn.Structures.Ftp
{
    /// <summary>
    /// FTP client specialized for Picture and Album classes
    /// </summary>
    class FTPFactory
    {
        /// <summary>
        /// FTP client class
        /// </summary>
        private FTPTransfer ftpTransfer;

        /// <summary>
        /// Constructor of class
        /// Initializes the FTP client
        /// </summary>
        public FTPFactory()
        {
            ftpTransfer = new FTPTransfer();
        }

        /// <summary>
        /// Uploads an album of a particulary user by uploading every album's picture
        /// </summary>
        /// <param name="username"> The album owner </param>
        /// <param name="album"> The album </param>
        /// <returns> TRUE if entire operation is completely done </returns>
        public bool UploadAlbum(String username, Album album)
        {
            bool actionCompleted = true;

            // Checks if user directory is already created
            if (!UserExists(username))
            {
                actionCompleted = CreateUser(username);
            }
            if (!actionCompleted)
                return false;
            // Checks if album directory is already created
            if (!AlbumExists(username, album.AlbumFolderName))
            {
                actionCompleted = CreateAlbum(username, album.AlbumFolderName);
            }

            // Uploads every picture from the album
            foreach (Picture picture in album.Pictures)
            {
                actionCompleted = UploadPicture(username, album, picture);
                if (!actionCompleted)
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Uploads only one picture
        /// </summary>
        /// <param name="username"></param>
        /// <param name="album"></param>
        /// <param name="picture"></param>
        /// <returns> TRUE if the picture is successfully stored on ftp server </returns>
        public bool UploadPicture(String username, Album album, Picture picture)
        {
            return ftpTransfer.UploadFile(username + "/" + album.AlbumFolderName + "/" + picture.PictureName,
                                          picture.PicturePath);
        }

        /// <summary>
        /// Download the pictures from ftp server "ftp://.../username/albumName"
        /// </summary>
        /// <param name="username"></param>
        /// <param name="albumName"></param>
        /// <param name="destinationPath"> The local directory path where the pictures will be downloaded </param>
        /// <returns> TRUE if the entire operation is successfully done </returns>
        public bool DownloadAlbum(String username, String albumName, String destinationPath)
        {
            bool actionCompleted = true;
            String albumServerPath = username + "/" + albumName;

            // Retrieves a list with the picture filenames
            List<String> pictureNames = ftpTransfer.GetFileNames(albumServerPath);
            if (pictureNames.Count == 0)
                return false;

            // Download picture by picture
            foreach (String pictureName in pictureNames)
            {
                String localPicturePath = destinationPath + @"\" + pictureName;
                actionCompleted = DownloadPicture(username, albumName, pictureName, localPicturePath);
                if (!actionCompleted)
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Downloads only one picture
        /// </summary>
        /// <param name="username"></param>
        /// <param name="albumName"></param>
        /// <param name="pictureName"></param>
        /// <param name="destinationPath"> The local path of the downloaded picture </param>
        /// <returns> TRUE if the picture is successfully stored on local computer </returns>
        public bool DownloadPicture(String username, String albumName, String pictureName, String destinationPath)
        {
            return ftpTransfer.DownloadFile(username + "/" + albumName + "/" + pictureName, destinationPath);
        }

        /// <summary>
        /// Checks if the user already has a folder on the ftp server
        /// </summary>
        /// <param name="username"></param>
        /// <returns> TRUE if the directory exists on server </returns>
        private bool UserExists(String username)
        {
            return ftpTransfer.DirectoryExists("", username);
        }

        /// <summary>
        /// Checks if the user already has a album stored on ftp server
        /// </summary>
        /// <param name="username"></param>
        /// <param name="albumName"></param>
        /// <returns> TRUE if the directory representing the album exists </returns>
        private bool AlbumExists(String username, String albumName)
        {
            return ftpTransfer.DirectoryExists(username, albumName);
        }

        /// <summary>
        /// Creates a user directory on the root directory
        /// </summary>
        /// <param name="username"></param>
        /// <returns> TRUE if the directory is successfully created </returns>
        private bool CreateUser(String username)
        {
            return ftpTransfer.CreateDirectory("", username);
        }

        /// <summary>
        /// Creates a album directory in the user directory
        /// </summary>
        /// <param name="username"></param>
        /// <param name="albumName"></param>
        /// <returns> TRUE if the album directory is successfully created </returns>
        private bool CreateAlbum(String username, String albumName)
        {
            return ftpTransfer.CreateDirectory(username, albumName);
        }
    }
}
